/**
 * Copyright (c) 2018-2099, Chill Zhuang 庄骞 (bladejava@qq.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.restaurant.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.restaurant.entity.UserSalaryDay;
import org.springblade.modules.restaurant.excel.UserSalaryDayExcel;
import org.springblade.modules.restaurant.service.IUserSalaryDayService;
import org.springblade.modules.restaurant.vo.UserSalaryDayVO;
import org.springframework.web.bind.annotation.*;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 用户日薪表 控制器
 *
 * @author Blade
 * @since 2025-02-18
 */
@RestController
@AllArgsConstructor
@RequestMapping("/restaurant/usersalaryday")
@Tag(name = "用户日薪表", description = "用户日薪表接口")
public class UserSalaryDayController extends BladeController {

	private IUserSalaryDayService userSalaryDayService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入userSalaryDay")
	public R<UserSalaryDay> detail(UserSalaryDay userSalaryDay) {
		UserSalaryDay detail = userSalaryDayService.getOne(Condition.getQueryWrapper(userSalaryDay));
		return R.data(detail);
	}

	/**
	 * 分页 用户日薪表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入userSalaryDay")
	public R<IPage<UserSalaryDay>> list(UserSalaryDayVO userSalaryDay, Query query) {
		IPage<UserSalaryDay> pages = userSalaryDayService.page(Condition.getPage(query), customizeQueryWrapper(userSalaryDay));
		return R.data(pages);
	}


	/**
	 * 自定义分页 用户日薪表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description = "传入userSalaryDay")
	public R<IPage<UserSalaryDayVO>> page(UserSalaryDayVO userSalaryDay, Query query) {
		IPage<UserSalaryDayVO> pages = userSalaryDayService.selectUserSalaryDayPage(Condition.getPage(query), userSalaryDay);
		return R.data(pages);
	}

	/**
	 * 新增 用户日薪表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description = "传入userSalaryDay")
	public R save(@Valid @RequestBody UserSalaryDay userSalaryDay) {
		// 判断是否已经有数据
		UserSalaryDay one = userSalaryDayService.getOne(Wrappers.<UserSalaryDay>lambdaQuery()
			.eq(UserSalaryDay::getUserId, userSalaryDay.getUserId())
			.eq(UserSalaryDay::getTime, userSalaryDay.getTime()));
		if (one != null) {
			return R.fail(one.getTime() + " - " + one.getUserName() + " 数据已存在");
		}
		return R.status(userSalaryDayService.saveUserSalaryDay(userSalaryDay));
	}

	/**
	 * 修改 用户日薪表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入userSalaryDay")
	public R update(@Valid @RequestBody UserSalaryDay userSalaryDay) {
		boolean isExist = userSalaryDayService.isExist(userSalaryDay.getId(),userSalaryDay.getUserId(), userSalaryDay.getTime());
		if (isExist) {
			return R.fail(userSalaryDay.getTime() + " - " + userSalaryDay.getUserName() + " 数据已存在");
		}
		return R.status(userSalaryDayService.updateById(userSalaryDay));
	}

	/**
	 * 新增或修改 用户日薪表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description = "传入userSalaryDay")
	public R submit(@Valid @RequestBody UserSalaryDay userSalaryDay) {
		boolean isExist = userSalaryDayService.isExist(userSalaryDay.getId(),userSalaryDay.getUserId(), userSalaryDay.getTime());
		if (isExist) {
			return R.fail(userSalaryDay.getTime() + " - " + userSalaryDay.getUserName() + " 数据已存在");
		}
		return R.status(userSalaryDayService.saveOrUpdate(userSalaryDay));
	}


	/**
	 * 删除 用户日薪表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "逻辑删除", description = "传入ids")
	public R remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(userSalaryDayService.deleteLogic(Func.toLongList(ids)));
	}

	@SneakyThrows
	@GetMapping("/export")
	@ApiOperationSupport(order = 8)
	@Operation(summary = "导出考勤记录", description = "传入userSalaryDay")
	public void export(UserSalaryDayVO userSalaryDay, HttpServletResponse response) {

		List<UserSalaryDay> salaryDays = userSalaryDayService.list(customizeQueryWrapper(userSalaryDay));
		List<UserSalaryDayExcel> list = userSalaryDayService.export(salaryDays);

		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding(StandardCharsets.UTF_8.name());
		String fileName = URLEncoder.encode("考勤记录", StandardCharsets.UTF_8)
			+ "_" + DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN);

		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		EasyExcel.write(response.getOutputStream(), UserSalaryDayExcel.class).sheet("考勤记录").doWrite(list);
	}
	private LambdaQueryWrapper<UserSalaryDay> customizeQueryWrapper(UserSalaryDayVO userSalaryDay) {
		String[] searchTime = userSalaryDay.getSearchTime();
		DateTime now = DateUtil.date();
		// 获取今天开始时间  yyyy-MM-dd
		String startTime = DateUtil.formatDate(now);
		// 获取当前时间 yyyy-MM-dd
		String endTime = DateUtil.formatDate(now);

		if (searchTime != null && searchTime.length == 2) {
			startTime = searchTime[0];
			endTime = searchTime[1];
		}
		return Wrappers.<UserSalaryDay>lambdaQuery()
			.eq(ObjUtil.isNotNull(userSalaryDay.getUserId()), UserSalaryDay::getUserId, userSalaryDay.getUserId())
			.eq(ObjUtil.isNotNull(userSalaryDay.getType()), UserSalaryDay::getType, userSalaryDay.getType())
			.between(UserSalaryDay::getTime, startTime, endTime).orderByAsc(UserSalaryDay::getTime);
	}
}
